define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
    require("cordova!com.justep.cordova.plugin.weixin.v3");
	var Model = function(){
	    this.timer = 0;
	    this.pointData = null;
	    this.fileList = [];
	    this.imgSelectIndex = -1;//当前选择的图片index
		this.callParent();
	};

   

	Model.prototype.modelLoad = function(event){
		this.comp("name").val(this.getParentModel().getParentModel().getUserName());
		this.comp("phone").val(this.getParentModel().getParentModel().getUserPhone());
		this.comp("address").val(this.getParentModel().getParentModel().getUserAddress());
		this.comp("input8").val("免费");
	};

    //选择要添加的图片
	Model.prototype.file2Change = function(event){
	    $(".preview img").off("click");
	    var me = this;
	    me.imgSelectIndex = me.fileList.length-1;
		this.fileList.push(event.currentTarget.files[0]);
		var src = window.URL.createObjectURL(event.currentTarget.files[0]);
		$("#"+this.getIDByXID("imgPreview")).append("<img src='"+src+"' style='margin-right:5px;' class='img-thumbnail imgUnSelect' />");
		$(".preview img").each(function(index){
			$(this).on("click",function(event){
				 me.imgSelectIndex = index;
				 $(this).toggleClass("imgUnSelect");
				 $(this).toggleClass("imgSelect");
				 $(this).siblings().removeClass("imgSelect");
				 $(this).siblings().addClass("imgUnSelect");
				 if($(this).hasClass("imgUnSelect")){
					 me.imgSelectIndex = -1;
				 }
			});
		});
		if(this.fileList.length == 1){
			$("#"+this.getIDByXID("deleteBtn")).show();
		}
	};

   
	//删除照片按钮

	Model.prototype.deleteBtnClick = function(event){
	    
	    	if(this.imgSelectIndex == -1){
	    		justep.Util.hint("请选择要删除的图片", {type:"danger"});    	
	    	}else{
	    		this.fileList.splice(this.imgSelectIndex,1);
		    	$(".preview img").eq(this.imgSelectIndex).remove();
		    	this.imgSelectIndex = this.fileList.length - 1;
		        if(this.fileList.length === 0){
		        	$("#"+this.getIDByXID("deleteBtn")).hide();
		        }
	    	}
	};
	
	
	Model.prototype.publish = function(event){
	    
		if(this.comp("textarea2").val() == ''){
			justep.Util.hint("内容不能为空", {type:"danger"});
		}else{
		      this.comp("popOver1").show();
			 var formData = new FormData();	 
			 
			 formData.append("name",this.comp("name").val());
			  formData.append("address",this.comp("address").val());
			 formData.append("phone",this.comp("phone").val());
			 formData.append("classify",this.comp("select2").val());
			 formData.append('userId',this.getParentModel().getParentModel().getUserId());
			 formData.append("describes",this.comp("textarea2").val());
			 for(var i = 0;i < this.fileList.length;i++){
				 formData.append("image"+i,this.fileList[i]);
			 }
			 
			 var me = this;
			 
			 $.ajax({
				  url: "http://www.xjzlhy.com:81/ex_community/a/api/community/myRelease",
				  type: "POST",
				  data: formData,
				  processData: false,  // 告诉jQuery不要去处理发送的数据
				  contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
				  success:function(result){
				      me.comp("popOver1").hide();
					  //justep.Shell.closePage();
				  }				  
				}); 
	
		}

	};
    


	return Model;
});